import { ok, fail } from '../utils/response'
import storeShopDeskRepository from '../database/repositories/storeShopDeskRepository'
import type { StoreShopDesk } from '../database/types'
import storeShopDeskOrderRepository, { StoreShopDeskOrderRow } from '../database/repositories/storeShopDeskOrderRepository'
import storeShopFoodRepository, { StoreShopFoodRow } from '../database/repositories/storeShopFoodRepository'
import storeShopFoodBindRepository, { StoreShopFoodBindRow } from '../database/repositories/storeShopFoodBindRepository'
import storeShopFoodSkuRepository, { StoreShopFoodSkuRow } from '../database/repositories/storeShopFoodSkuRepository'
import storeShopPrinterRepository, { StoreShopPrinterRow } from '../database/repositories/storeShopPrinterRepository'
import storeShopComputerRepository, { StoreShopComputerRow } from '../database/repositories/storeShopComputerRepository'

export class DeskService {
    static batchUpsert(desks: StoreShopDesk[]) {
        try {
            storeShopDeskRepository.upsertBatch(desks)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'batch upsert failed')
        }
    }

    static list() {
        try {
            return ok(storeShopDeskRepository.list())
        } catch (e: any) {
            return fail(e?.message || 'list failed')
        }
    }

    static get(id: string) {
        try {
            return ok(storeShopDeskRepository.getById(id))
        } catch (e: any) {
            return fail(e?.message || 'get failed')
        }
    }

    static delete(ids: string[]) {
        try {
            storeShopDeskRepository.deleteByIds(ids)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'delete failed')
        }
    }

    // 订单：批量新增/更新
    static orderBatchUpsert(rows: StoreShopDeskOrderRow[]) {
        try {
            storeShopDeskOrderRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'order batch upsert failed')
        }
    }

    // 订单：查询
    static orderListByStore(store_id: string, shop_id?: string) {
        try {
            return ok(storeShopDeskOrderRepository.listByStore(store_id, shop_id))
        } catch (e: any) {
            return fail(e?.message || 'order list failed')
        }
    }

    // 菜品：批量新增/更新
    static foodBatchUpsert(rows: StoreShopFoodRow[]) {
        try {
            storeShopFoodRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'food batch upsert failed')
        }
    }

    // 菜品：查询
    static foodListByStore(store_id: string, category_id?: string) {
        try {
            return ok(storeShopFoodRepository.listByStore(store_id, category_id))
        } catch (e: any) {
            return fail(e?.message || 'food list failed')
        }
    }

    // 估清绑定：批量新增/更新
    static foodBindBatchUpsert(rows: StoreShopFoodBindRow[]) {
        try {
            storeShopFoodBindRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'food bind batch upsert failed')
        }
    }

    // 估清绑定：查询
    static foodBindListByStore(store_id: string, shop_id?: string) {
        try {
            return ok(storeShopFoodBindRepository.listByStore(store_id, shop_id))
        } catch (e: any) {
            return fail(e?.message || 'food bind list failed')
        }
    }

    // SKU：批量新增/更新
    static foodSkuBatchUpsert(rows: StoreShopFoodSkuRow[]) {
        try {
            storeShopFoodSkuRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'food sku batch upsert failed')
        }
    }

    // SKU：按菜品查询
    static foodSkuListByFood(food_id: string) {
        try {
            return ok(storeShopFoodSkuRepository.listByFood(food_id))
        } catch (e: any) {
            return fail(e?.message || 'food sku list failed')
        }
    }

    // 打印机：批量新增/更新
    static printerBatchUpsert(rows: StoreShopPrinterRow[]) {
        try {
            storeShopPrinterRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'printer batch upsert failed')
        }
    }

    // 打印机：查询
    static printerListByStore(store_id: string, shop_id?: string) {
        try {
            return ok(storeShopPrinterRepository.listByStore(store_id, shop_id))
        } catch (e: any) {
            return fail(e?.message || 'printer list failed')
        }
    }

    // 电脑：批量新增/更新（仅落库关键信息，其余留空）
    static computerBatchUpsert(rows: StoreShopComputerRow[]) {
        try {
            storeShopComputerRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'computer batch upsert failed')
        }
    }

    // 电脑：查询
    static computerListByStore(store_id: string, shop_id?: string) {
        try {
            return ok(storeShopComputerRepository.listByStore(store_id, shop_id))
        } catch (e: any) {
            return fail(e?.message || 'computer list failed')
        }
    }
}

export default DeskService


